/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
//作者:AKPower
//算法:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 6100;
ll dp[maxn][2], val[maxn], n;
ll fa[maxn];
vector<ll> p[maxn];
bool isch[maxn];

void dfs(ll u)
{
    dp[u][0] = 0;
    dp[u][1] = val[u];
    ll len = p[u].size();
    for (ll i = 0; i < len; i++)
    {
        ll v = p[u][i];
        dfs(v);
        dp[u][0] += max(dp[v][0], dp[v][1]);
        dp[u][1] += dp[v][0];
    }
    return;
}

int main()
{
    IOS;
    cin >> n;
    for (ll i = 1; i <= n; i++)
    {
        cin >> val[i];
    }
    ll x, y;
    for (ll i = 1; i <= n - 1; i++)
    {
        cin >> x >> y;
        isch[x] = true;
        fa[x] = y;
        p[y].push_back(x);
    }
    ll root = 1;
    for (ll i = 1; i <= n; i++)
    {
        if (isch[i])
            continue;
        dfs(i);
        root = i;
    }
    cout << max(dp[root][0], dp[root][1]) << endl;
    getchar();
    getchar();
    return 0;
}
